<template>
  <el-container>
    <el-header>
      <div>电商后台管理系统</div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
  <!-- 侧边栏-->
      <el-aside :width="isCollapse === true ? '64px' : '200px' ">
<!--        折叠按钮-->
        <div class="toggle" @click="toggleChange">|||</div>
<!--        左侧导航菜单-->
        <el-menu
          background-color="#373d41"
          text-color="#fff"
          active-text-color="#ffd04b"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="activePath">
<!--          一级导航-->
          <el-submenu :index=" item.id + ''" v-for=" item in menuList" :key="item.id">
            <template slot="title">
              <i class="el-icon-location"></i>
              <span>{{item.authName}}</span>
            </template>
<!--            二级导航-->
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveStatePath('/' + subItem.path)">
              <template slot="title">
                <i class="el-icon-location"></i>
                <span>{{subItem.authName}}</span>
            </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'Home',
  data() {
    return {
      //左侧菜单数据
      menuList: [],
      // 是否折叠
      isCollapse: false,
      //被激活的连接
      activePath: ''
    }
  },
  created() {
    this.getMenus()
    window.sessionStorage.getItem('path')
  },
  methods:{
    logout () {
      // 清理token,然后重定向到登录页面
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取左侧导航区域数据
    async getMenus() {
      let { data : res } = await this.$http.get(`menus`)
      this.menuList = res.data
      // console.log(this.menuList)
    },
    toggleChange() {
      //点击折叠按钮时,折叠左侧菜单
      this.isCollapse = !this.isCollapse
    },
    saveStatePath(activePath) {
      //点击二级菜单时,将用户点击的路径保存到sessionStorage中
      window.sessionStorage.setItem('path', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style scoped lang="less">
  .el-container{
    height: 100%;
    .el-header{
      background-color: #373d41;
      display: flex;
      justify-content: space-between;
      align-items: center;
      >div{
        font-size: 20px;
        color: white;
      }
    }
    .el-aside{
      background-color: #373d41;
      .toggle{
        background-color: #373d41;
        color: #fff;
        text-align: center;
        letter-spacing: 0.2em;
        cursor: pointer;
      }
    }
    .el-main{
      background-color: #eee;
    }
  }
  .el-menu{
    border-right: none;
  }
</style>
